/**
 * 文  件   名：HWMenuPopupWindow.java
 * 版          权： Copyright PingAn Technology All Rights Reserved.
 * 描          述：
 * 创  建   人： LIJINHUA922   E-mail:LIJINHUA922@pingan.com.cn
 * 创建时间： 2015-1-19 上午10:41:48
 * <p>
 * 修   改  人：
 * 修改时间：
 * 修改内容：[修改内容]
 */
package com.app.jzgdirector.common.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;


import com.app.jzgdirector.R;

import java.util.List;


public class MineBankCardSelectWindow extends PopupWindow {


    private Context context;
    private OnMenuItemClick menuItemClick;

    private ListView mBankCardListView;
    private List<String> datas;

    public interface OnMenuItemClick {
        public void onMenuItemClick(String bankType);
    }

    public MineBankCardSelectWindow(final Context context, List<String> datas) {
        super(context);
        this.context = context;
        this.datas = datas;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View layoutView = inflater.inflate(R.layout.mine_hp_select_bank_card_window, null);

        mBankCardListView = (ListView) layoutView.findViewById(R.id.mBankCardListView);
        mBankCardListView.setAdapter(new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, datas));

        // 设置SelectPicPopupWindow的View
        this.setContentView(layoutView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
//		 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        // this.setAnimationStyle(R.style.PopupAnimation);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mBankCardListView.setAnimation(AnimationUtils.loadAnimation(context,
                R.anim.push_bottom_in));
        layoutView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = layoutView.findViewById(R.id.mBankCardListView).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        mBankCardListView.setAnimation(AnimationUtils.loadAnimation(
                                context, R.anim.push_bottom_out));
                        popuwindowNewDismiss();
                    }
                }
                return true;
            }
        });

        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                popuwindowNewDismiss();
            }
        });

        mBankCardListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                menuItemClick.onMenuItemClick(datas.get(i));
                popuwindowNewDismiss();
            }
        });
    }

    public void setOnMenuItemClick(OnMenuItemClick menuItemClick) {
        this.menuItemClick = menuItemClick;
    }

    /**
     *
     */
    private void popuwindowNewDismiss() {
        this.setFocusable(false);
        if (isShowing()) {
            dismiss();
        }
    }


}
